/**
 * 
 */
package com.zzgele.qmzb.api.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * @author Martin
 * 
 */
public class DateUtil {
	/**
	 * 获取系统当前时间戳
	 * @return
	 */
	public static String getCurUnixTimeStamp(){
		long unixTimeStamp = System.currentTimeMillis() /1000L;
		return String.valueOf(unixTimeStamp);
	}

	/**
	 * 获取当前时间戳
	 * @return
	 */
	public static Integer getCurUnixTimeStampInt(){
		return Integer.valueOf(getCurUnixTimeStamp());
	}

	/**
	 * 把unix时间戳转换为时间字符串 [格式：“yyyy-MM-dd HH:mm:ss”]
	 * @param timeStamp 时间戳
	 * @return 格式化好的时间字符串
	 */
	public static String toDate(long timeStamp) {
	    return toDate(timeStamp, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 把unix时间戳转换为时间字符串 [格式：“yyyy-MM-dd”]
	 * 
	 * @param timeStamp
	 *            时间戳
	 * @return 格式化好的时间字符串
	 */
	public static String toDateShort(long timeStamp) {
		return toDate(timeStamp, "yyyy-MM-dd");
	}

	/**
	 * 把unix时间戳转换为时间字符串
	 * @param timeStamp 时间戳
	 * @param dateFormat 时间格式 yyyy-MM-dd HH:mm:ss a
	 * @return 格式化好的时间字符串
	 */
	public static String toDate(long timeStamp, String dateFormat) {
		if (timeStamp == 0) {
			return "";
		}
		timeStamp = timeStamp * 1000;
		SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        String result = format.format(new Date(timeStamp));
		return result;
	}

	/**
	 * 根据生日计算年龄
	 * @param birthday 生日字符串【yyyy-MM-dd】
	 * @return
	 */
	public static int getAgeByBirth(String birthday) {
		int age = 0;
		if (!ValidateUtil.isDate(birthday)) {
			return age;
		}
		try {
			Calendar now = Calendar.getInstance();
			now.setTime(new Date());// 当前时间

			Calendar birth = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date birthdayDate = sdf.parse(birthday);
			birth.setTime(birthdayDate);

			if (birth.after(now)) {//如果传入的时间，在当前时间的后面，返回0岁
				age = 0;
			} else {
				age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
				if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
					age += 1;
				}
			}
			return age;
		} catch (Exception e) {//兼容性更强,异常后返回数据
			return age;
		}
	}

	public static Integer getUnixTimeStampByDate(Date date) {
		Long ts = Long.valueOf(getCurUnixTimeStampInt());
		try {
			ts = date.getTime() / 1000 + (8*60*60);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Integer.valueOf(ts.toString());
	}



    /**
    * 获取当前时间的 年月日时分秒毫秒
    */
    public static String getCurrentTime(){
        Calendar caldendar = Calendar.getInstance();

        String year = String.valueOf(caldendar.get(Calendar.YEAR));
        String month = String.valueOf(caldendar.get(Calendar.MONTH)+1);
        String day = String.valueOf(caldendar.get(Calendar.DATE));
        String hour = String.valueOf(caldendar.get(Calendar.HOUR_OF_DAY));
        String minute = String.valueOf(caldendar.get(Calendar.MINUTE));
        String sencond = String.valueOf(caldendar.get(Calendar.SECOND));
        String millisecond = String.valueOf(caldendar.get(Calendar.MILLISECOND));

        String fileName = year + prefixString(month) + prefixString(day) + prefixString(hour) + prefixString(minute) + prefixString(sencond) + prefixString2(millisecond);
        while (fileName.length() < 17){
            fileName = fileName + "0";
        }
        return fileName;
    }

    private static String prefixString(String s) {
        if (s.length() == 1) {
            s = "0" + s;
        }
        return s;
    }

    private static String prefixString2(String s) {
        if (s.length() == 1) {
            s = "00" + s;
        } else if (s.length() == 2) {
            s = "0" + s;
        }
        return s;
    }

	/**
	 * 转换成format
	 */
	public static String dateToStr(Date d, String format) {
		if(d == null) return "";
		return new SimpleDateFormat(format).format(d);
	}
	/**
	 * 将指定的日期加上指定的天数
	 */
	public static Date addDays(Date d, int days) {
		if(days == 0) return d;
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.add(Calendar.DATE, days);
		setNoTime(c);
		return c.getTime();
	}
	/**
	 * 把指定日期的时分秒设为0
	 * @param c 指定的日期
	 */
	private static void setNoTime(Calendar c) {
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
	}
    /**
     * 获取随机的数值。
     * @param length	长度
     * @return
     */
    public static String getRandom(Integer length){
        String result = "";
        Random rand = new Random();
        int n = 20;
        if(null != length && length > 0){
            n = length;
        }
        boolean[]  bool = new boolean[n];
        int randInt = 0;
        for(int i = 0; i < length ; i++) {
            do {
                randInt  = rand.nextInt(n);

            }while(bool[randInt]);

            bool[randInt] = true;
            result += randInt;
        }
        return result;
    }


    /**
     * 把时间字符串转换为时间戳
     * @param time 时间格式 yyyy-MM-dd HH:mm:ss
     * @return 10位Integer类型时间戳
     */
    public static Integer date2TimeStamp(String time){
        Integer timeStemp = 0;
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat .parse(time);
            timeStemp = Integer.parseInt(String.valueOf(date.getTime()/1000));
        }catch (Exception e){
            e.printStackTrace();
        }
        return timeStemp;
    }

	/**
	 * 判断当前时间是否为指定直播时间
	 * @return
	 */
	public static boolean allowLiveBroadcast () {

		try {

			SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
			Date now =null;
			Date beginTime = null;
			Date endTime = null;

            now = df.parse(df.format(new Date()));
            beginTime = df.parse("00:00");
            endTime = df.parse("24:00");
//			beginTime = df.parse("20:00");
//			endTime = df.parse("24:00");

			Calendar date = Calendar.getInstance();
			date.setTime(now);

			Calendar begin = Calendar.getInstance();
			begin.setTime(beginTime);

			Calendar end = Calendar.getInstance();
			end.setTime(endTime);

			if (date.after(begin) && date.before(end)) {
				return true;
			} else {
				return false;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return false;

	}

	public static void main(String[] args) {
		System.out.println(allowLiveBroadcast());
	}


}
